package com.hwadee.teachingprocessmanagementsystem.util;

import java.io.File;
import java.util.*;

/**
 * @Description: 文件工具类
 * @Author: Jukomu
 * @Package: com.hwadee.teachingprocessmanagementsystem.util
 * @Project: TeachingProcessManagementSystem
 * @name: FileUtils
 * @Date: 2024/7/15-10:54
 * @Filename: FileUtils
 */
public class FileUtils {
    public static Map<String,List<File>> listFiles(File path) {
        Map<String,List<File>> map = new HashMap<String,List<File>>();
        List<File> files = new ArrayList<File>();
        List<File> paths = new ArrayList<File>();
        List<File> allFiles = new ArrayList<File>();
        try {
            allFiles = new java.util.ArrayList<>(List.of(path.listFiles()));
        } catch (Exception ignored){
        }
        if (!allFiles.isEmpty()) {
            for (File file : allFiles) {
                if (file.isDirectory()) {
                    paths.add(file);
                    try {
                        files.addAll(listFiles(file).get("files"));
                    } catch (Exception ignored){}
                    try {
                        paths.addAll(listFiles(file).get("paths"));
                    } catch (Exception ignored){}
                } else {
                    files.add(file);
                }
            }
        } else {
            return null;
        }
        map.put("files", files);
        map.put("paths", paths);
        return map;
    }
}
